local skel = fk.CreateSkill {
  name = "emo__shanglue",
}

Fk:loadTranslationTable{
  ["emo__shanglue"] = "商略",
  [":emo__shanglue"] = "准备阶段，你可以与一名其他角色交换手牌，若其手牌数：减少，结束阶段再与之交换；增加，其可以使用一张牌。",

  ["#emo__shanglue-choose"] = "商略：你可以与一名其他角色交换手牌",
  ["@[chara]emo__shanglue-turn"] = "商略",
  ["#emo__shanglue-use"] = "商略：可以使用一张牌",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(skel.name) and player.phase == Player.Start
  end,
  on_cost = function (self, event, target, player, data)
    local targets = table.filter(player.room:getOtherPlayers(player, false), function (p)
      return not p:isKongcheng() and not player:isKongcheng()
    end)
    if #targets == 0 then return false end
    local tos = player.room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, targets = targets, skill_name = skel.name,
      prompt = "#emo__shanglue-choose",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local num = to:getHandcardNum()
    room:swapAllCards(player, {player, to}, skel.name)
    if to.dead then return end
    if to:getHandcardNum() > num then
      room:askToPlayCard(to, {
        prompt = "#emo__shanglue-use", skill_name = skel.name, skip = false, pattern = ".",
      })
    elseif not player.dead then
      room:setPlayerMark(player, "@[chara]emo__shanglue-turn", to.id)
    end
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player == target and player:getMark("@[chara]emo__shanglue-turn") ~= 0 and player.phase == Player.Finish
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(player:getMark("@[chara]emo__shanglue-turn"))
    room:setPlayerMark(player, "@[chara]emo__shanglue-turn", 0)
    if to and not to.dead then
      room:doIndicate(player, {to})
      room:swapAllCards(player, {player, to}, skel.name)
    end
  end,
})

return skel
